package pl.edu.mimuw.irs.rcp.controller.editor.resources;

import org.eclipse.gef.EditDomain;
import org.eclipse.ui.views.properties.IPropertyDescriptor;
import org.eclipse.ui.views.properties.IPropertySource;
import org.eclipse.ui.views.properties.PropertyDescriptor;
import org.eclipse.ui.views.properties.TextPropertyDescriptor;

import pl.edu.mimuw.irs.core.model.design.resource.IMember;
import pl.edu.mimuw.irs.core.model.design.resource.Resource;
import pl.edu.mimuw.irs.rcp.controller.editor.util.AbstractAdaptableDataWrapper;
import pl.edu.mimuw.irs.rcp.controller.editor.util.IMemberWrapper;
import pl.edu.mimuw.irs.rcp.util.MessageCode;
import pl.edu.mimuw.irs.rcp.util.UITexts;

public class AdaptableResourceWrapper extends AbstractAdaptableDataWrapper<Resource> implements IMemberWrapper {

	// *** Attributes
	
	private ResourcePropertySource resourceSource;

	// *** Constructors

	public AdaptableResourceWrapper(Resource data, EditDomain editDomain) {
		super(data, editDomain);
	}
	
	// *** Methods
	
	@Override
	public IMember getMember() {
		return getData();
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public Object getAdapter(Class adapter) {
		if (adapter == IPropertySource.class) {
			if (resourceSource == null) {
				resourceSource = new ResourcePropertySource(
						getData().getName(), getData().getUnitCost());
			}
			return resourceSource;
		}
		return null;
	}
	
	public class ResourcePropertySource implements IPropertySource {

		// *** Attributes
		
		private static final String PROPERTY_NAME = "properties.resource.name";
		private static final String PROPERTY_COST = "properties.resource.cost";
		
		private IPropertyDescriptor[] propertyDescriptors;
		
		private String defaultName;
		private int defaultCost;
		
		// *** Constructors
		
		public ResourcePropertySource(String defaultName, int defaultCost) {
			this.defaultName = defaultName;
			this.defaultCost = defaultCost;
		}
	
		// *** Methods
		
		@Override
		public Object getEditableValue() {
			return null;
		}

		@Override
		public IPropertyDescriptor[] getPropertyDescriptors() {
			if (propertyDescriptors == null) {
	            // Create a descriptors and set a category
	            PropertyDescriptor nameDescriptor = 
	            	new TextPropertyDescriptor(PROPERTY_NAME, UITexts.get(MessageCode.PROPERTIES_FIELD_NAME));

	            PropertyDescriptor costDescriptor = 
	            	new TextPropertyDescriptor(PROPERTY_COST, UITexts.get(MessageCode.PROPERTIES_FIELD_COST_UNIT));

	            propertyDescriptors = new IPropertyDescriptor[] { 
	            		nameDescriptor, costDescriptor };
            }
            return propertyDescriptors;
		}

		@Override
		public Object getPropertyValue(Object id) {
			if (PROPERTY_NAME.equals(id)) {
				return getData().getName();
			} else if (PROPERTY_COST.equals(id)) {
				return String.valueOf(getData().getUnitCost());
			}
			return null;
		}

		@Override
		public boolean isPropertySet(Object id) {
			if (PROPERTY_NAME.equals(id)) {
	        	String curValue = (String)getPropertyValue(id);
				return !curValue.equals(defaultName);
			} else if (PROPERTY_COST.equals(id)) {
	        	String curValue = (String)getPropertyValue(id);
				return !curValue.equals(String.valueOf(defaultCost));
			}
			return false;
		}

		@Override
		public void resetPropertyValue(Object id) {
			if (PROPERTY_NAME.equals(id)) {
				setPropertyValue(id, defaultName);
			} else if (PROPERTY_COST.equals(id)) {
				setPropertyValue(id, String.valueOf(defaultCost));
			}
		}

		@Override
		public void setPropertyValue(Object id, Object value) {
			if (PROPERTY_NAME.equals(id)) {
				getData().setName((String)value);
			} else if (PROPERTY_COST.equals(id)) {
				try {
					getData().setUnitCost(Integer.valueOf((String)value));
				} catch (NumberFormatException nfe) {}
			}
		}
		
	}
}
